package com.example.spingbootmybatis.controller;


import com.example.spingbootmybatis.mapper.AdminMapper;
import com.example.spingbootmybatis.mapper.UserMapper;
import com.example.spingbootmybatis.pojo.admin;
import com.example.spingbootmybatis.pojo.user;
import com.example.spingbootmybatis.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class JumpController {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UserService userService;

    @RequestMapping("/jumptomain")
    public String  tomain(){
        return "dashboard";
    }

    @RequestMapping("/jumptouserlist")
    public String touserlist(Model model, HttpSession session){

        List<user> users = userMapper.queryUserList();
        System.out.println(users);
        String userwho = (String) session.getAttribute("userwho");
        String userpwd = (String) session.getAttribute("userpwd");

        admin admin = adminMapper.queryAdminByNP(userwho, userpwd);

        if (admin!=null) {
            model.addAttribute("UserList", users);
            model.addAttribute("mespermission","你是管理员，为你展示全部权限");
            return "operate/userlist";
        }
        else {
            model.addAttribute("error1","不是管理员，权限不足");
            //return "redirect:/jumptochakanliuyan";   //不是管理员转到查看
            return "operate/chakan";
        }
    }

    @RequestMapping("/jumptocomment")
    public String ToComment(@RequestParam("id") int id,Model model){
        user user = userMapper.queryUserById(id);
        model.addAttribute("user",user);
        System.out.println("git");
        return "operate/comment";
    }

    @RequestMapping("/jumptochakanliuyan")
    public String chakanfenye(ModelMap map,Model model){
        List<user> users = userMapper.queryUserList();
        System.out.println(users);
        model.addAttribute("UserList",users);
        return "operate/chakan";
//        @RequestParam(defaultValue = "1") int pageNum,
//        @RequestParam(defaultValue = "5") int pageSize,

//        System.out.println(pageNum);
//        System.out.println(pageSize);
//        //把查询的表给前端
//        List<user> users = userService.queryUserListByPage(pageNum,pageSize);
//        map.addAttribute("UserList",users);
//
//        //实现分页功能
//        PageInfo<user> userPageInfo = new PageInfo<>(users);
//        model.addAttribute("pageInfo",userPageInfo);
//        return "/operate/chakan";

    }
//    public String ToChaKanLiuYan(Model model){
//        List<user> users = userMapper.queryUserList();
//        System.out.println(users);
//        model.addAttribute("UserList",users);
//        return "/operate/chakan";


//    }

//    //尝试使用分页查询
//    @RequestMapping("/page")
//    public String page(@RequestParam(defaultValue = "1") int pageNum,
//                       @RequestParam(defaultValue = "5") int pageSize,
//                       ModelMap map,Model model){
//        System.out.println(pageNum);
//        System.out.println(pageSize);
//        //把查询的表给前端
//        List<user> users = userService.queryUserListByPage(pageNum,pageSize);
//        map.addAttribute("UserList",users);
//
//        //实现分页功能
//        List<user> users1 = userService.queryUserListByPage(pageNum, pageSize);
//        PageInfo<user> userPageInfo = new PageInfo<>(users1);
//        model.addAttribute("pageInfo",userPageInfo);
//        return "/operate/chakan";
//
//    }

    @RequestMapping("/jumptoliuyan")
    public String toLiuYan(){
        return "operate/liuyan";
    }

    @RequestMapping("/jumptoadminregister")
    public String jumptoadminregister(){
        return "operate/adminregister";
    }

    @ResponseBody
    @RequestMapping("/jiekou")
    public List<user> jiekou(){
        return userMapper.queryUserList();
    }
}
